package com.example.androidlearning2400.ui.act

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.androidlearning2400.R
import com.example.androidlearning2400.data.Datasource
import com.example.androidlearning2400.ui.act.ui.theme.AndroidLearning2400Theme
import com.example.androidlearning2400.ui.model.EntryBean
import kotlin.random.Random


class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AndroidLearning2400Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
                    MainApp()
                }
            }
        }
    }
}

@Composable
fun MainApp() {
    EntryList(
        msgList = Datasource().loadEntryData(),
    )
}

@Composable
fun EntryList(msgList: List<EntryBean>, modifier: Modifier = Modifier) {
    LazyColumn(modifier = modifier.padding(top = 56.dp)) {
        items(msgList) { entryBean ->
            EntryItem(entryBean = entryBean)
            Spacer(modifier = Modifier.height(2.dp))
        }
    }
}

/**
 * 生成随机颜色值
 */
fun randomColor(): Color {
    val red = Random.nextInt(256) // 0-255
    val green = Random.nextInt(256)
    val blue = Random.nextInt(256)
    return Color(red, green, blue)
}

@Composable
fun EntryItem(entryBean: EntryBean) {
    val context = LocalContext.current
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .background(Color.White)
            .padding(horizontal = 12.dp, vertical = 8.dp)
            .clickable {
                val intent = Intent(context, entryBean.targetActivity)
                context.startActivity(intent)
            }) {

        Spacer(
            modifier = Modifier
                .width(24.dp)
                .height(24.dp)
                .background(randomColor())
        )

        Column(
            modifier = Modifier.padding(horizontal = 16.dp)
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 2.dp)
            ) {
                Text(
                    text = entryBean.actName,
                    fontSize = 16.sp,
                    fontWeight = FontWeight.Medium,
                    color = Color(0xFF333333)
                )
                Text(
                    text = entryBean.date, fontSize = 14.sp, color = Color(0xFFAAAAAA)
                )
            }
            Text(
                text = entryBean.actDesc, fontSize = 14.sp, color = Color(0xFFAAAAAA)
            )
        }
    }
}

@Preview
@Composable
private fun EntryItemPreview() {
    EntryItem(
        EntryBean(
            "金甲工坊", "今天下午3点在技能训练中心开周例会", "11:30", LoginActivity::class.java
        ),
    )
}
